<template>
    <div class="taskWrapper" v-loading="loading">
        <div class="taskHeader">
            <div class="left" @click="handleBack">
                <el-icon>
                    <ArrowLeft style="font-size:24px" />
                </el-icon>
                <span class="leftBack">返回</span>
            </div>
            <div class="header-center">学习任务</div>
        </div>
        <div class="taskBox">
            <div class="taskMineInfo">
                <div class="task-container">
                    <div class="top">
                    <TaskDesc :desc="dataDetail.description"/>
                    <TaskExamInfo :examList="dataDetail.paperList"/>
                    <TaskDownload :downloadList="dataDetail.fileList"/>
                    <TaskUpload :file-list="dataDetail.recordList" :upload-url="uploadUrl" :eid="dataDetail.id" @updateFileList="handleUpdateFileList" />
                    <!-- <TaskUpload :file-list="dataDetail.recordList" :upload-url="uploadUrl" :eid="dataDetail.id" @updateFileList="handleUpdateFileList" /> -->
                </div>
                <el-divider />
                    <div class="task-footer">
                       <div class="left-footer">作业更新于{{updateTime}}</div>
                       <div class="right-footer">
                        <span class="update-time-text">点击确认作业完成情况</span>
                        <el-button type="primary" @click="handleComplete">完成</el-button>
                       </div>
                    </div>
                </div>

            </div>
        </div>
    </div>
</template>
<script setup>
import { useRouter, useRoute } from 'vue-router'
import TaskHeader from './TaskHeader.vue';
import TaskExamInfo from './TaskExamInfo.vue';
import TaskUpload from '@/components/TaskUpload.vue'
import TaskDesc from './TaskDesc.vue'
import TaskDownload from './TaskDownload.vue';
import { myHomeworkDetail, myHomeworkSubmit } from '@/service/personalCenter';
import dayjs from 'dayjs';
const route = useRouter();
const routeParams = useRoute();
console.log(routeParams.query.id,'routeParams.query.id')
const homeworkId = ref(Number(routeParams.query.id));
const loading=ref(false);
const dataDetail=ref({});
const uploadUrl = ref('https://admin.testace.cn/fenglingClient/client/homework/file/add');
definePageMeta({
    layout: 'task',
});
const handleBack=()=>{
    route.back();
}
const handleUpdateFileList = (newFileList) => {
  dataDetail.value.recordList = newFileList;
}
const updateTime=ref('')

// 获取作业详情
const getHomeworkDetail = async () => {
  try {
    const params = {
      id: homeworkId.value
    };
    loading.value = true;
    const res = await myHomeworkDetail(params);
    if (res && res.code === '000000') {
      const data = res.data;
      dataDetail.value = data;
      updateTime.value = dayjs(data.hand_time * 1000).format('YYYY-MM-DD HH:mm:ss');
    }
    loading.value = false;
  } catch (error) {
    loading.value = false;
    console.error('获取作业详情失败', error);
  }
};

const handleComplete = async () => {
  try {
    const params = {
      id: homeworkId.value
    };
    const res = await myHomeworkSubmit(params);
    if (res && res.code === 200) {
      // 提交成功
      ElMessage.success('作业提交成功');
    }
  } catch (error) {
    console.error('作业提交失败', error);
  }
};

onMounted(()=>{
    getHomeworkDetail();
})
</script>
<style lang="scss" scoped>
.taskWrapper {
    background-color: #EAF4F2;

    .taskHeader {
        background: #FFFFFF;
        width: 100%;
        height: 64px;
        padding: 20px;
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;

        .left {
            display: flex;
            position: absolute;
            top: 50%;
            left: 20px;
            transform: translate(0px, -50%);
            align-items: center;

            .leftBack {
                font-size: 14px;
                font-weight: 400;
                color: #333333;
            }
        }

        .header-center {
            color: #333333;
            font-size: 18px;
            font-weight: 600
        }


    }


    .taskBox {

        display: flex;
        justify-content: center;

        .taskMineInfo {
            width: 1000px;
            margin: 20px 0px;
            background-color: #FFFFFF;
            border-radius: 8px;
            .top{
                padding: 20px;
            }
        }
    }
}
.task-footer{
    padding:20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left-footer{
        font-family: PingFang SC;
font-size: 14px;
font-weight: 400;
line-height: 19.6px;
text-align: left;
text-underline-position: from-font;
text-decoration-skip-ink: none;
color:#333333;
    }
    .right-footer{
        display: flex;
        align-items: center;
        .update-time-text{
            font-family: PingFang SC;
font-size: 12px;
font-weight: 400;
line-height: 16.8px;
text-align: left;
text-underline-position: from-font;
text-decoration-skip-ink: none;
color:#999;margin-right:10px;
        }
    }
}
:deep(.el-divider--horizontal){
    margin:0px;
}
</style>